
import axios from "axios"
import {message} from "antd"



const AUTH_TOKEN = "";
// export const  baseURL  = "http://120.26.73.36:2010/"
export const  baseURL  = "http://localhost:2010/"

axios.defaults.baseURL = baseURL;    // 服务接口的 基记录 
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.timeout =  60000;


export function ShowLoading(){
    message.loading("请求中...")
}

export function ShowSuccess(msg){
    message.destroy()
    msg = msg || "请求成功"
    message.success({
        content:msg,
        duration:2
    })
}

export function ShowFail(msg){
    message.destroy()
    msg = msg || "未知错误,请联系管理员"
    message.error({
        content:msg,
        duration:2
    })
      
}


// 拦截器Interceptors

// 请求发送之前 
axios.interceptors.request.use(function (config) {

    ShowLoading();
    const server_token = localStorage.getItem("token")
    // console.log(server_token)
    config.headers.common['_token'] = server_token;

    
    return config;
}, function (error) {
    ShowFail("请求失败")
    return Promise.reject(error);
});

// 请求响应完成 
axios.interceptors.response.use(function (response) {
    console.log(response.data)
    if(response.data.code==200){
        ShowSuccess(response.data.msg)
    }else if(response.data.code==402 || response.data.code==404 || response.data.code==500 || response.data.code==503 ){
        ShowFail(response.data.msg)
    }else if(response.data.code==3000){
        ShowFail(response.data.msg)
    }

    return response;
}, function (error) {
    ShowFail("响应失败")
    return Promise.reject(error);
});

// get 请求 
export const get = function(url,params,headers){
    return new Promise(function(resolve,reject){
        axios.get(url,{
            params,
            headers
        })
        .then(res=>{
            resolve(res.data)  // data 消除
        })
        .catch(err=>{
            reject(err)
        })
    })
}

// POST  请求 
export const post = function(url,body,headers,params){
    return new Promise(function(resolve,reject){
        axios.post(url,body,{
            params,
            headers
        })
        .then(res=>{
            resolve(res.data)  // data 消除
        })
        .catch(err=>{
            reject(err)
        })
    })
}